/*
 * MyGraph.java
 *
 * Created on June 8, 2007, 9:06 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.jerm.Interface.JGraph_Algebra;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.text.Position;
import org.jgraph.JGraph;
import org.jgraph.graph.CellMapper;
import org.jgraph.graph.DefaultPort;
import org.jgraph.graph.Edge;
import org.jgraph.graph.EdgeView;
import org.jgraph.graph.GraphLayoutCache;
import org.jgraph.graph.GraphModel;

/**
 * JGraph Levimente Modificado
 * @author Tiago Falcao
 */
public class MyGraph extends JGraph {

        JFileChooser fileChooser;
    
        /**
         * Construct the Graph using the Model as its Data Source
         * @param model Modelo de JGraph
         */
        public MyGraph(GraphModel model) {
                this(model, null);
        }

        /**
         * Construct the Graph using the Model as its Data Source
         * @param model Modelo de JGraph
         * @param cache Cache do JGraph
         */
        public MyGraph(GraphModel model, GraphLayoutCache cache) {
                super(model, cache);
                // Make Ports Visible by Default
                //setPortsVisible(true);
                // Use the Grid (but don't make it Visible)
                setGridEnabled(true);
                // Set the Grid Size to 10 Pixel
                setGridSize(6);
                // Set the Tolerance to 2 Pixel
                setTolerance(2);
                // Accept edits if click on background
                setInvokesStopCellEditing(true);
                // Allows control-drag
                setCloneable(true);
                // Jump to default port on connect
                setJumpToDefaultPort(true);
        }

    
    
    public void exportImage(){
		int returnValue = JFileChooser.CANCEL_OPTION;
		initFileChooser();
		returnValue = fileChooser.showSaveDialog(this);
		if (returnValue == JFileChooser.APPROVE_OPTION) {
                    int inset=1;
            try {
                OutputStream out = new FileOutputStream(fileChooser.getSelectedFile());
                Color bg = null; // Use this to make the background transparent
                bg = this.getBackground(); // Use this to use the graph background color
                BufferedImage img = this.getImage(bg, inset);
                ImageIO.write(img, "png", out);
                out.flush();
                out.close();
            } catch (FileNotFoundException ex) {
                new Error("Arquivo não Encontrado");
            } catch (IOException ex) {
                new Error("Error ao gravar Imagem");
            }
		}

    }
    
    
    
    	public void initFileChooser() {
		if (fileChooser == null) {
			fileChooser = new JFileChooser();
			FileFilter fileFilter = new FileFilter() {
				/**
				 * @see javax.swing.filechooser.FileFilter#accept(File)
				 */
				public boolean accept(File f) {
					if (f == null)
						return false;
					if (f.getName() == null)
						return false;
					if (f.getName().endsWith(".png"))
						return true;
					if (f.isDirectory())
						return true;

					return false;
				}

				/**
				 * @see javax.swing.filechooser.FileFilter#getDescription()
				 */
				public String getDescription() {
					return "Imagem PNG";
				}
			};
			fileChooser.setFileFilter(fileFilter);
		}
	}
}
